package com.uzhie.jt808.jt1078.entity;

import com.uzhie.jt808.JT808MsgBody;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.jt808.msg.entity.MyBuffer;

/**
 * 远程视频复播控制
 * Created by lilongfei on 2018/7/30.
 */
public class JT_9202 implements JT808MsgBody {

    //逻辑通道号
    byte channel;
    //远程复播控制 0 开始回放 1 暂停回放 2 结束回放 3 快进回放 4 关键帧快退回放 5 拖动回放 6 关键帧播放
    byte control;
    //快进快退倍数 回放控制3或4时,该字段有效,其他情况为0
    //0\无效 1\1倍 2\2倍 3\4倍 4\8倍 5\16倍
    byte multiple;
    //回放控制为5时,该字段有效,时间类型 ,格式为 YYMMDDHHMMSS
    String location;

    public JT_9202(int channel,int control,int multiple,String location){
        setChannel(channel);
        setControl(control);
        setMultiple(multiple);
        setLocation(location);
    }

    /**
     * 设置通道号
     * @param channel
     */
    public void setChannel(int channel){
        byte[] channels = BitConverter.GetBytes(channel);
        setChannel(channels[3]);
    }

    /**
     * 设置播放控制
     * @param control
     */
    public void setControl(int control){
        byte[] controls = BitConverter.GetBytes(control);
        setControl(controls[3]);
    }

    /**
     * 设置快退快进
     * @param multiple
     */
    public void setMultiple(int multiple){
        byte[] multiples = BitConverter.GetBytes(multiple);
        setMultiple(multiples[3]);
    }

    public byte getChannel() {
        return channel;
    }

    public void setChannel(byte channel) {
        this.channel = channel;
    }

    public byte getControl() {
        return control;
    }

    public void setControl(byte control) {
        this.control = control;
    }

    public byte getMultiple() {
        return multiple;
    }

    public void setMultiple(byte multiple) {
        this.multiple = multiple;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    @Override
    public byte[] WriteToBytes() {
        MyBuffer buffer = new MyBuffer();
        buffer.put(getChannel());
        buffer.put(getControl());
        buffer.put(getMultiple());
        //设置拖动时间
        buffer.put(Byte.parseByte(getLocation().substring(2, 4), 16));
        buffer.put(Byte.parseByte(getLocation().substring(5, 7), 16));
        buffer.put(Byte.parseByte(getLocation().substring(8, 10), 16));
        buffer.put(Byte.parseByte(getLocation().substring(11, 13), 16));
        buffer.put(Byte.parseByte(getLocation().substring(14, 16), 16));
        buffer.put(Byte.parseByte(getLocation().substring(17, 19), 16));
        return buffer.array();
    }

    @Override
    public void ReadFromBytes(byte[] body) {
        MyBuffer buffer = new MyBuffer(body);
        setChannel(buffer.get());
        setControl(buffer.get());
        setMultiple(buffer.get());
        byte[] time = buffer.gets(6);
        setLocation("20" + String.format("%02X", time[0]) + "-"
                + String.format("%02X", time[1]) + "-"
                + String.format("%02X", time[2]) + " "
                + String.format("%02X", time[3]) + ":"
                + String.format("%02X", time[4]) + ":"
                + String.format("%02X", time[5]));
    }
}
